<template>
  <div class="profile-container">
    <div class="profile-card">
      <!-- 头部区域 -->
      <div class="profile-header">
        <div class="avatar-wrapper">
          <img
              :src="data.user.avatar || 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'"
              alt="用户头像"
              class="avatar"
          />
        </div>
        <h2 class="user-name">{{ data.user.name }}</h2>
        <div class="user-id">账号：{{ data.user.username }}</div>
      </div>

      <!-- 信息区域 -->
      <div class="info-section">
        <div class="info-group">
          <div class="info-item">
            <el-icon><User /></el-icon>
            <div class="info-content">
              <label>用户姓名</label>
              <span>{{ data.user.name }}</span>
            </div>
          </div>

          <div class="info-item">
            <el-icon><Male /></el-icon>
            <div class="info-content">
              <label>性别</label>
              <span>{{ getGender(data.user.gender) }}</span>
            </div>
          </div>

          <div class="info-item">
            <el-icon><Calendar /></el-icon>
            <div class="info-content">
              <label>出生日期</label>
              <span>{{ data.user.birthdate }}</span>
            </div>
          </div>

          <div class="info-item">
            <el-icon><Phone /></el-icon>
            <div class="info-content">
              <label>手机号</label>
              <span>{{ data.user.phoneNumber }}</span>
            </div>
          </div>
        </div>
      </div>

      <!-- 操作按钮 -->
      <div class="action-section">
        <el-button type="primary" class="edit-btn" @click="handleEdit">
          <el-icon><Edit /></el-icon>
          编辑信息
        </el-button>
      </div>
    </div>

    <!-- 编辑对话框 -->
    <el-dialog
        v-model="data.formVisible"
        title="编辑个人信息"
        width="500px"
        :close-on-click-modal="false"
        destroy-on-close
        class="edit-dialog"
    >
      <el-form
          label-width="120px"
          :model="data.form"
          :rules="rules"
          ref="userRef"
          class="edit-form"
      >
        <el-form-item label="头像" prop="avatar">
          <el-upload
              :action="uploadUrl"
              list-type="picture"
              :before-upload="handleBeforeUpload"
              :on-success="handleImgSuccess"
              class="avatar-uploader"
          >
            <el-button type="primary">
              <el-icon><Upload /></el-icon>
              上传图片
            </el-button>
          </el-upload>
        </el-form-item>

        <el-form-item label="用户姓名" prop="name">
          <el-input v-model="data.form.name" placeholder="请输入用户姓名"/>
        </el-form-item>

        <el-form-item label="性别" prop="gender">
          <el-radio-group v-model="data.form.gender">
            <el-radio label="Male">男</el-radio>
            <el-radio label="Female">女</el-radio>
            <el-radio label="Other">其他</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="出生日期" prop="birthdate">
          <el-date-picker
              v-model="data.form.birthdate"
              value-format="YYYY-MM-DD"
              format="YYYY-MM-DD"
              type="date"
              placeholder="选择日期"
              style="width: 100%"
          />
        </el-form-item>

        <el-form-item label="手机号" prop="phoneNumber">
          <el-input v-model="data.form.phoneNumber" placeholder="请输入手机号"/>
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="data.formVisible = false">取消</el-button>
          <el-button type="primary" @click="submitForm">保存</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import {reactive, ref} from 'vue'
import request from '@/utils/request'
import {ElMessage} from 'element-plus'
import {hideLoading, showLoading} from "@/utils/LoadingUtil"
import {
  User,
  Male,
  Calendar,
  Phone,
  Edit,
  Upload
} from '@element-plus/icons-vue'

const uploadUrl = import.meta.env.VITE_BASE_URL + '/files/upload'

const data = reactive({
  user: JSON.parse(localStorage.getItem('system-user') || '{}'),
  formVisible: false,
  form: {}
})

const handleBeforeUpload = async () => {
  showLoading("上传中...")
}

const handleImgSuccess = (file) => {
  hideLoading()
  data.form.avatar = file.data
  data.user.avatar = file.data
  ElMessage.success('头像上传成功')
}

const emit = defineEmits(['updateUser'])

const save = () => {
  showLoading("保存中...")
  request.put('/user/update', data.form).then((res) => {
    hideLoading()
    if (res.code === '200') {
      ElMessage.success('更新成功')
      request.get('/user/selectById/' + data.user.id).then((res) => {
        if (res.code === '200') {
          data.user = res.data
          localStorage.setItem('system-user', JSON.stringify(res.data))
          emit('updateUser')
        } else {
          ElMessage.error(res.msg)
        }
      })
      data.formVisible = false
    } else {
      ElMessage.error(res.msg)
    }
  }).catch(() => {
    hideLoading()
    ElMessage.error('保存失败，请重试')
  })
}

const handleEdit = () => {
  data.formVisible = true
  data.form = JSON.parse(JSON.stringify(data.user))
}

const userRef = ref(null)
const submitForm = () => {
  userRef.value.validate((valid) => {
    if (valid) {
      save()
    } else {
      ElMessage.error('请填写必填项')
      return false
    }
  })
}

const rules = {
  name: [
    {required: true, message: '请输入用户姓名', trigger: 'blur'},
    {min: 2, max: 15, message: '用户姓名长度需在2-15个字符之内', trigger: 'blur'}
  ],
  gender: [{required: true, message: '请选择性别', trigger: 'change'}],
  birthdate: [{required: true, message: '请选择出生日期', trigger: 'change'}],
  phoneNumber: [
    {required: true, message: '请输入手机号', trigger: 'blur'},
    {pattern: /^1[3-9]\d{9}$/, message: '手机号格式有误', trigger: 'blur'}
  ]
}

const getGender = (gender) => {
  return gender === 'Male' ? '男' : gender === 'Female' ? '女' : '其他'
}
</script>

<style>

.profile-container {
  min-height: 100vh;
  padding: 40px 20px;
  background: linear-gradient(135deg, #f5f7fa 0%, #ffffff 100%);
  display: flex;
  justify-content: center;
  align-items: flex-start;
}

.profile-container .profile-card {
  width: 100%;
  max-width: 800px;
  background: white;
  border-radius: 24px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.08);
  overflow: hidden;
  position: relative;
}

.profile-container .profile-header {
  padding: 60px 40px;
  background: linear-gradient(135deg, #409EFF 0%, #64B5F6 100%);
  text-align: center;
  color: white;
  position: relative;
}

.profile-container .avatar-wrapper {
  position: relative;
  width: 180px;
  height: 180px;
  margin: 0 auto 20px;
  border-radius: 50%;
  border: 6px solid rgba(255, 255, 255, 0.2);
  transition: all 0.3s ease;
  overflow: hidden;
}

.profile-container .avatar {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  object-fit: cover;
  transition: all 0.3s ease;
}

.profile-container .user-name {
  font-size: 32px;
  font-weight: 600;
  margin: 0;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.profile-container .user-id {
  font-size: 16px;
  opacity: 0.8;
  margin-top: 8px;
}

.profile-container .info-section {
  padding: 40px;
}

.profile-container .info-group {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 30px;
}

.profile-container .info-item {
  display: flex;
  align-items: center;
  padding: 20px;
  background: #f8fafc;
  border-radius: 16px;
  transition: all 0.3s ease;
}

.profile-container .info-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.06);
}

.profile-container .info-item .el-icon {
  font-size: 24px;
  color: #409EFF;
  margin-right: 20px;
}

.profile-container .info-content {
  flex: 1;
}

.profile-container .info-content label {
  display: block;
  font-size: 14px;
  color: #909399;
  margin-bottom: 4px;
}

.profile-container .info-content span {
  font-size: 18px;
  color: #2c3e50;
  font-weight: 500;
}

.profile-container .action-section {
  padding: 0 40px 40px;
  text-align: center;
}

.profile-container .edit-btn {
  height: 50px;
  padding: 0 40px;
  font-size: 16px;
  font-weight: 500;
  border-radius: 12px;
  background: linear-gradient(135deg, #409EFF 0%, #64B5F6 100%);
  border: none;
  box-shadow: 0 4px 15px rgba(64, 158, 255, 0.2);
  transition: all 0.3s ease;
}

.profile-container .edit-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(64, 158, 255, 0.3);
}

.profile-container .edit-btn .el-icon {
  margin-right: 8px;
}

/* 对话框样式 */
.profile-container .edit-dialog .el-dialog {
  border-radius: 24px;
  overflow: hidden;
}

.profile-container .edit-dialog .el-dialog__header {
  padding: 30px;
  background: linear-gradient(135deg, #409EFF 0%, #64B5F6 100%);
  margin-right: 0;
}

.profile-container .edit-dialog .el-dialog__title {
  color: white;
  font-size: 20px;
  font-weight: 500;
}

.profile-container .edit-dialog .el-dialog__body {
  padding: 40px;
}

.profile-container .edit-dialog .el-dialog__footer {
  padding: 20px 40px;
  border-top: 1px solid #eee;
}

.profile-container .edit-form .el-form-item {
  margin-bottom: 25px;
}

.profile-container .edit-form .el-input__wrapper {
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
  border-radius: 12px;
}

.profile-container .edit-form .el-input__wrapper:hover,
.profile-container .edit-form .el-input__wrapper.is-focus {
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
}

.profile-container .avatar-uploader {
  text-align: center;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .profile-container .profile-card {
    border-radius: 16px;
  }

  .profile-container .profile-header {
    padding: 40px 20px;
  }

  .profile-container .avatar-wrapper {
    width: 140px;
    height: 140px;
  }

  .profile-container .user-name {
    font-size: 24px;
  }

  .profile-container .info-section {
    padding: 20px;
  }

  .profile-container .info-group {
    grid-template-columns: 1fr;
    gap: 15px;
  }

  .profile-container .info-item {
    padding: 15px;
  }

  .profile-container .action-section {
    padding: 0 20px 20px;
  }

  .profile-container .edit-dialog .el-dialog {
    width: 90% !important;
    margin: 0 auto;
  }

  .profile-container .edit-dialog .el-dialog__body {
    padding: 20px;
  }
}
</style>
